var express = require('express');
var getTicket = require('../getjsapiticket');
var wxconfig = require('../wxconfig');
var sha1 = require('sha1');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
    res.render('testing',{title:'试管成功率',ishead:true });
});

router.get('/index', function(req, res, next) {
    res.render('testIndex',{title:'试管成功率',ishead:true});
});

router.get('/result', function(req, res, next) {
    var rate = req.query.rate;
    res.render('testResult',{title:'试管成功率',ishead:true,rate:rate  });
});

router.get("/ticket",function (req,res,next) {
    getTicket().then(function (ticket) {
            var page = decodeURIComponent(req.query.page);
            var t = {};
            var ticket = ticket;
            t.appid = wxconfig.appid;
            t.noncestr = sha1(new Date());
            // t.noncestr = '8dee76fc2aeb21490465130722300ed8affa6d59';
            t.timestamp = parseInt(new Date().getTime() / 1000);
            // t.timestamp = 1525250352;
            var string = 'jsapi_ticket=' + ticket + '&noncestr=' + t.noncestr + '&timestamp=' + t.timestamp + '&url=' + page;

            t.signature = sha1(string);
            res.json({t:JSON.stringify(t)});
    })
})

module.exports = router;
